<?php
/**
 * 分包单位资格报审表控制器
 *@author ZhouYP
 */
namespace Admin\Controller;
use Common\Controller\AuditbaseController;
class SubcontractorQualificationController extends AuditbaseController{

	    public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/SubcontractorQualification");
		$this->sc_model =D("Common/SubcontractContent");
		
		$this->check = array('code');
		// 当前审批流程
		$this->function = "contract";
		$this->subfunction = "3";
	}
	
	/**
     *  主页
     */
	public function index(){
		/**搜索条件**/
		$where = "d.is_deleted <> 1 ";		
		$this->indexAuditPage($where);
	}

	public function add_post(){
		if(IS_POST){
			$this->dataCheck();
            $data = array_merge($_POST, $this->saveData());
            if ($this->deal_model->create()!==false) {
                $result = $this->deal_model->add($data);
                if ($result!==false) {
					// 创建审批 一级流程数据
					$history = array(
						'table_id'			=> $result,
						'audit_status'		=> 1,
						'audit_reason'		=> '新建表单',
						'amount_in_words'	=> '',
						'audit_level'		=> 1
					);
					$data = array_merge($history, $this->auditAdd());
					$this->auditHistoryModel->add($data);
					
					$dept["table_id"] = $result;
					for($i = 0; $i < count($_POST['subcontract_position']); $i++){
						$dept['subcontract_position'] = $_POST['subcontract_position'][$i];
						$dept['quantity'] = $_POST['quantity'][$i];
						$dept['contract_amount'] = $_POST['contract_amount'][$i];
						
						if($dept['subcontract_position'] == "" 
							|| $dept['quantity'] == ""
							|| $dept['contract_amount'] == ""){
							continue;	
						}
						$this->sc_model->add($dept);
					};

                    $this->success("添加成功！", U("SubcontractorQualification/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
	}

	/**
     *  删除 
     */
	public function delete(){
	    $id = I('get.id', 0, 'intval');
		if(!$id){
			$this->error("请选择要删除的数据！");
		}
		$data = array(
			'id'	=> $id,
			'is_deleted' => 1
		);
		if ($this->deal_model->save($data) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

	//分配权限页面
	public function distribution(){
		$level = $_GET['level'];
		//var_dump($level);
		$this->assign("level", $level);
		$this->distribution_base();
	}

}
?>